package com.zjs.jsp.mongodbapi.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjs.jsp.domain.jsp.Jsp_Order;
import com.zjs.jsp.domain.stats.JspStatus;
import com.zjs.jsp.mongodbapi.service.MqProducer;
import com.zjs.jsp.mongodbapi.service.domain.JspPocList;
import com.zjs.jsp.mongodbapi.service.mongodb.EdiOrderRepository;
import com.zjs.jsp.mongodbapi.service.mongodb.JspTaskExpand;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 李文
 * @create 2017-09-08 16:22
 **/
@RestController
public class TaskApiController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskApiController.class);

    @Autowired
    ObjectMapper objectMapper;

    @Value("${axdTopic}")
    private String axdTopic;

    @Value("${axdTags}")
    private String axdTags;

    @Autowired
    private MqProducer mqProducer;

    @Autowired
    private JspTaskExpand jspTaskExpand;


    @Autowired
    private EdiOrderRepository e;


    @ApiOperation(value = "状态回传", notes = " 返回字符串  Y  N  ")
    @ApiImplicitParam(name = "jspStatus", value = "数据", required = true, dataType = "JspStatus", paramType = "body")
    @PostMapping("/update/status")
    public String updateTask(@RequestBody JspStatus jspStatus) {

        LOGGER.info(" 安鲜达 订单状态回传  接受的参数是 " + jspStatus.toString());
        String out = "N";
        String id = jspStatus.getTaskCode() + " " + jspStatus.getOrderCode();


        try {
            if ("40".equals(jspStatus.getStatus())) {
                List<String> list = new ArrayList<>();
                String[] ts;
                if (jspStatus.getTaskCode() != null) {
                    ts = jspStatus.getTaskCode().split(",");
                    Collections.addAll(list, ts);
                } else {
                    ts = jspStatus.getOrderCode().split(",");
                    list = getPocs(ts);
                }

                if (jspTaskExpand.updateCzzt(list, 40)) {
                    LOGGER.info(id + " 更新任务包状态为 派送 成功 ");
                } else {
                    LOGGER.info(id + " 更新任务包状态为 派送 失败  返回信息：" + out);
                    return out;
                }
            }

            if ("50".equals(jspStatus.getStatus())) {
                if (StringUtils.isEmpty(jspStatus.getOrderCode())) {
                    return out;
                }
                String[] ts = jspStatus.getOrderCode().split(",");
                List<String> list = getPocs(ts);
                if (jspTaskExpand.updateCzzt(list, "50")) {
                    LOGGER.info(id + " 更新任务包状态为 签收 成功 ");
                } else {
                    LOGGER.info(id + " 更新任务包状态为 签收 失败  返回信息：" + out);
                    return out;
                }
            }
        } catch (Exception e) {
            LOGGER.error(id + " 发送信息到MQ 异常 异常信息：" + e.getCause(), e);
            LOGGER.info(id + "返回信息：" + out);
            return out;
        }
        try {
            String data = objectMapper.writeValueAsString(jspStatus);
            if (mqProducer.send(id, data) != null) {
                out = "Y";
            }
        } catch (Exception e) {
            LOGGER.error(id + " 发送信息到MQ 异常 异常信息：" + e.getCause(), e);
        }
        LOGGER.info(id + "返回信息：" + out);
        return out;

    }



    @ApiOperation(value = "重新下发", notes = " 返回字符串  Y  N  ")
    @ApiImplicitParam(name = "pocs", value = "数据", required = true, dataType = "JspPocList", paramType = "body")
    @PostMapping("/update/xfzt")
    public String updateXfzt(@RequestBody JspPocList pocs) {
        try {
            if (jspTaskExpand.updateXfzt(pocs.getPocs())) {
                return "Y";
            }
        } catch (Exception e) {
            LOGGER.error(pocs.toString() + " 重新下发失败 " + e.getMessage(), e);
        }
        return "N";
    }
    
    private List<String> getPocs(String[] ts) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, ts);
        List<Jsp_Order> jist = e.findByMidIn(list);
        for (int i = 0; i < ts.length; i++) {
            String t = ts[i];
            for (Jsp_Order j : jist) {
                if (t.equals(j.getMid())) {
                    ts[i] = j.getPoc();
                }
            }
        }
        return list;
    }

}
